//
//  DeliveryViewController.m
//  Hui
//
//  Created by HawkingWang on 16/10/26.
//  Copyright © 2016年 Hui. All rights reserved.
//

#import "DeliveryViewController.h"
#import "MessageViewController.h"//消息Controller
#import "MineViewController.h"//我的Controller
#import "DoneDeliveryTableViewCell.h"//已完成Cell
#import "BeingTableViewCell.h"//正在配送Cell
#import "GetOrderNavView.h"//导航栏View

#import "GetOrderDetailViewController.h"//订单详情Controller

@interface DeliveryViewController ()<UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate>

@property (nonatomic, strong) UITableView *beingTableView;
@property (nonatomic, strong) UITableView *doneTableView;

@end

@implementation DeliveryViewController
{
    GetOrderNavView *_navView;
    
    UIScrollView *_scrollView;
    UIButton *_being;
    UIButton *_done;
    UIView *_slider;
}
#define SliderWidth (SCREEN_WIDTH / 2)

 static NSString *const BeingTableViewCellID = @"BeingTableViewCell";
 static NSString *const DoneDeliveryTableViewCellID = @"DoneDeliveryTableViewCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    //设置导航栏
    [self setNav];
    
    //设置HeaderView&ScrollView
    [self setHeaderViewandScrollView];
    
    //注册TableView
    [self registerTableView];
}

- (void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.hidden = YES;
}


//#pragma mark === 设置导航栏 ===
//- (void)setNav{
//    UIButton *mine = [UIButton buttonWithType:UIButtonTypeCustom];
//    mine.frame = CGRectMake(0, 0, 59, 44);
//    [mine addTarget:self action:@selector(jumpToMine) forControlEvents:UIControlEventTouchUpInside];
//    UIImageView *mineImage;
//    //6p
//    if (SCREEN_WIDTH == 414){
//        mineImage = [[UIImageView alloc] initWithFrame:CGRectMake(-5, 7.5, 29, 29)];
//    }else{
//        mineImage = [[UIImageView alloc] initWithFrame:CGRectMake(-1, 7.5, 29, 29)];
//    }
//    mineImage.image = [UIImage imageNamed:@"top-my"];
//    mineImage.layer.cornerRadius = 14.5;
//    mineImage.layer.masksToBounds = YES;
//    mineImage.alpha = 0.9;
//    [mine addSubview:mineImage];
//    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:mine];
//    self.navigationItem.leftBarButtonItem = leftItem;
//    
//    UIButton *message = [UIButton buttonWithType:UIButtonTypeCustom];
//    message.frame = CGRectMake(0, 0, 59, 44);
//    [message addTarget:self action:@selector(jumpToMessage) forControlEvents:UIControlEventTouchUpInside];
//    UIImageView *messageImage;
//    //6p
//    if (SCREEN_WIDTH == 414){
//        messageImage = [[UIImageView alloc] initWithFrame:CGRectMake(35, 7.5, 29, 29)];
//    }else{
//        messageImage = [[UIImageView alloc] initWithFrame:CGRectMake(31, 7.5, 29, 29)];
//    }
//    messageImage.image = [UIImage imageNamed:@"top-message"];
//    messageImage.layer.cornerRadius = 14.5;
//    messageImage.layer.masksToBounds = YES;
//    messageImage.alpha = 0.9;
//    [message addSubview:messageImage];
//    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:message];
//    self.navigationItem.rightBarButtonItem = rightItem;
//    
//    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 76, 30)];
//    lable.text = @"我的配送";
//    lable.textAlignment = NSTextAlignmentCenter;
//    lable.textColor = [UIColor whiteColor];
//    lable.font = [UIFont fontWithName:APPFONT size:19];
//    self.navigationItem.titleView = lable;
//    
//    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 59, 44)];
//    view.backgroundColor = [UIColor clearColor];
//    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:view];
//    backItem.title = @"";
//    self.navigationItem.backBarButtonItem = backItem;
//}

#pragma mark === 设置导航栏 ===
- (void)setNav{
    _navView = [[[NSBundle mainBundle] loadNibNamed:@"GetOrderNavView" owner:nil options:nil] lastObject];
    _navView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 64);
    _navView.backgroundColor = APPCOLOR;
    _navView.title.text = @"我的配送";
    [self.view addSubview:_navView];
    __weak __typeof__ (self)weakSelf = self;
    _navView.NavClick = ^(int num){
        switch (num) {
            case 0:
                [weakSelf jumpToMine];
                break;
            case 1:
                [weakSelf jumpToMessage];
                break;
            default:
                break;
        }
    };
}



#pragma mark === 设置HeaderView&ScrollView ===
- (void)setHeaderViewandScrollView{
    //HeaderView
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, 44)];
    [self.view addSubview:headerView];
    
    _being = [UIButton buttonWithType:UIButtonTypeCustom];
    _being.frame = CGRectMake(0, 0, SCREEN_WIDTH / 2, 44);
    [_being setTitle:@"正在配送" forState:UIControlStateNormal];
    _being.titleLabel.font = [UIFont fontWithName:APPFONT size:15];
    [_being setTitleColor:APPCOLOR forState:UIControlStateNormal];
    [_being addTarget:self action:@selector(being) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:_being];
    
    _done = [UIButton buttonWithType:UIButtonTypeCustom];
    _done.frame = CGRectMake(SCREEN_WIDTH / 2, 0, SCREEN_WIDTH / 2, 44);
    [_done setTitle:@"已完成" forState:UIControlStateNormal];
    _done.titleLabel.font = [UIFont fontWithName:APPFONT size:15];
    [_done setTitleColor:RGBCOLOR(73, 83, 90) forState:UIControlStateNormal];
    [_done addTarget:self action:@selector(done) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:_done];
    
    _slider = [[UIView alloc] initWithFrame:CGRectMake(0, 43, SliderWidth, 1)];
    _slider.backgroundColor = [UIColor whiteColor];
    [headerView addSubview:_slider];
    UIView *sliderr = [[UIView alloc] initWithFrame:CGRectMake(39, 0, SliderWidth - 39 * 2, 1)];
    sliderr.backgroundColor = APPCOLOR;
    [_slider addSubview:sliderr];
    
    //ScrollView
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 44 + 64, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _scrollView.delegate = self;
    _scrollView.contentSize = CGSizeMake(SCREEN_WIDTH * 2, SCREEN_HEIGHT - 44 - 64 - 49);
    _scrollView.pagingEnabled = YES;//按页滚动
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.backgroundColor = RGB(236, 236, 236);
    [self.view addSubview:_scrollView];
}
- (void)being{
    [_being setTitleColor:APPCOLOR forState:UIControlStateNormal];
    [_done setTitleColor:RGBCOLOR(73, 83, 90) forState:UIControlStateNormal];
    [UIView animateWithDuration:0.2 animations:^{
        _slider.frame = CGRectMake(0, 43, SliderWidth, 1);
    } completion:^(BOOL finished) {
        _scrollView.contentOffset = CGPointMake(0, 0);
    }];
}
- (void)done{
    [_being setTitleColor:RGBCOLOR(73, 83, 90) forState:UIControlStateNormal];
    [_done setTitleColor:APPCOLOR forState:UIControlStateNormal];
    [UIView animateWithDuration:0.2 animations:^{
        _slider.frame = CGRectMake(SCREEN_WIDTH / 2, 43, SliderWidth, 1);
    } completion:^(BOOL finished) {
        _scrollView.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
    }];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView == _scrollView) {
        CGFloat x = scrollView.contentOffset.x / 2;
        if (scrollView.contentOffset.x <= 0) {
            _slider.frame = CGRectMake(0, 43, SliderWidth, 1);
        }else if (scrollView.contentOffset.x >= SCREEN_WIDTH){
            _slider.frame = CGRectMake(SCREEN_WIDTH / 2, 43, SliderWidth, 1);
        }else{
            _slider.frame = CGRectMake(x, 43, SliderWidth, 1);
        }
        if (scrollView.contentOffset.x > SliderWidth) {
            [_being setTitleColor:RGBCOLOR(73, 83, 90) forState:UIControlStateNormal];
            [_done setTitleColor:APPCOLOR forState:UIControlStateNormal];
        }else{
            [_being setTitleColor:APPCOLOR forState:UIControlStateNormal];
            [_done setTitleColor:RGBCOLOR(73, 83, 90) forState:UIControlStateNormal];
        }
    }
}

- (void)registerTableView{
    [self.beingTableView registerNib:[UINib nibWithNibName:@"BeingTableViewCell" bundle:nil] forCellReuseIdentifier:BeingTableViewCellID];
    [self.doneTableView registerNib:[UINib nibWithNibName:@"DoneDeliveryTableViewCell" bundle:nil] forCellReuseIdentifier:DoneDeliveryTableViewCellID];
}

#pragma mark === UITableViewDelegate ===
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == _beingTableView) {
        return 3;
    }else return 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == _beingTableView) {
        if (indexPath.row == 0) {
            return 170;
        }
        return 160;
    }else{
        if (indexPath.row == 0) {
            return 120;
        }
        return 110;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //正在配送
    if (tableView == _beingTableView) {
        BeingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:BeingTableViewCellID];
//        [cell loadData:<#(BeingModel *)#>];
        //确认取货
        if (indexPath.row == 0) {
            cell.confirm.backgroundColor = [UIColor whiteColor];
            [cell.confirm setTitle:@"确认收货" forState:UIControlStateNormal];
            [cell.confirm setTitleColor:RGBCOLOR(253, 151, 82) forState:UIControlStateNormal];
            cell.cancleOrder.hidden = NO;
            cell.state.text = @"待取货";
            cell.contactLabel.text = @"联系派单人";
            cell.contactPerson.text = @"梁女士";
        }
        //确认送达
        else{
            cell.confirm.backgroundColor = RGBCOLOR(253, 151, 52);
            [cell.confirm setTitle:@"确认送达" forState:UIControlStateNormal];
            [cell.confirm setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            cell.cancleOrder.hidden = YES;
            cell.state.text = @"待配送";
            cell.contactLabel.text = @"联系收货人";
            cell.contactPerson.text = @"王汉卿";
        }
        
        cell.cellClick = ^(int num){
            switch (num) {
                case 0:
                    if (indexPath.row == 0) {
                        DLog(@"确认取货");
                    }else{
                        DLog(@"确认送达");
                    }
                    break;
                case 1:
                    DLog(@"联系");
                    break;
                case 2:
                    DLog(@"取消订单");
                    break;
                default:
                    break;
            }
        };
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    //已完成
    else{
        DoneDeliveryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:DoneDeliveryTableViewCellID];
//        [cell loadData:<#(DoneDeliveryModel *)#>];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    GetOrderDetailViewController *get = [[GetOrderDetailViewController alloc] init];
    get.hidesBottomBarWhenPushed = YES;
    //正在配送
    if (tableView == _beingTableView) {
        if (indexPath.row == 0) {
            get.state = 1;
        }else{
            get.state = 2;
        }
    }
    //已完成
    else{
        get.state = 3;
    }
    [self.navigationController pushViewController:get animated:YES];
}


- (void)jumpToMine{
    DLog(@"我的");
    //    LoginWithPhoneViewController *login = [[LoginWithPhoneViewController alloc] init];
    //    login.hidesBottomBarWhenPushed = YES;
    //    [self.navigationController pushViewController:login animated:YES];
    MineViewController *mine = [[MineViewController alloc] init];
    mine.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:mine animated:YES];
}
- (void)jumpToMessage{
    DLog(@"消息");
    MessageViewController *message = [[MessageViewController alloc] init];
    message.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:message animated:YES];
}

- (UITableView *)beingTableView{
    if (!_beingTableView) {
        _beingTableView= [[UITableView alloc]initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 44 - 49)style:UITableViewStylePlain];
        _beingTableView.delegate = self;
        _beingTableView.dataSource = self;
        _beingTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _beingTableView.showsVerticalScrollIndicator = NO;//去掉滑动竖条
        _beingTableView.backgroundColor = [UIColor clearColor];
        [_scrollView addSubview:_beingTableView];
    }
    return _beingTableView;
}
- (UITableView *)doneTableView{
    if (!_doneTableView) {
        _doneTableView= [[UITableView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH,0, SCREEN_WIDTH, SCREEN_HEIGHT - 44 - 64 - 49)style:UITableViewStylePlain];
        _doneTableView.delegate = self;
        _doneTableView.dataSource = self;
        _doneTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _doneTableView.showsVerticalScrollIndicator = NO;//去掉滑动竖条
        _doneTableView.backgroundColor = [UIColor clearColor];
        [_scrollView addSubview:_doneTableView];
    }
    return _doneTableView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
